package com.lq.learningTest.duoxiancheng;

public class Deamon {

    public static void main(String[] args) {
        //mian方法的主线程创建了t1线程，t1线程创建了t2线程
        Thread t1 = new Thread(() -> {
            int count = 10;
            Thread t2 = new Thread(() -> {
                while (true){
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("我是个守护线程！");
                }
            });
            //设置t2为t1的守护线程
            t2.setDaemon(true);
            t2.start();

            while (count > 0){
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("我是用户线程！");
                count--;
            }
            System.out.println("用户线程结束-------------------");
        });
//        t1.setDaemon(true);//把t1设置为main主线程的守护线程，当main方法运行完后，会结束t1，而t1又会结束自己的守护线程t2,即t2还没有开始就结束了
        t1.start();//t1用户线程结束后，t2守护线程也会结束
    }
}